<!--
 * @Author: your name
 * @Date: 2021-07-04 12:09:53
 * @LastEditTime: 2021-07-07 11:59:34
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue-hr-project\src\views\departments\index.vue
-->
<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <!-- 头部布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>操作</el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <!-- :default-expand-all="true"   true 的时候默认展开 -->
        <el-tree :data="list" :props="defaultProps" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (table 单元格数据渲染)-->
          <!-- 只要使用插槽,就使用固定的template模板包裹html模板  #default  #具名的name   -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDepartment(data)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDepartment(data)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delDepartments(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>

    <!-- 添加子部门组件 -->
    <AddDepartment
      ref="editDep"
      :code-list="codeList"
      :cur-date="curDate"
      :show-dialog="showDialog"
      @close-dialog="closeDialog"
      @update-depart="loadDepartmentList"
    />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transTreeData } from '@/utils/transTreeData'
import AddDepartment from './components/add-department.vue'
export default {
  components: {
    AddDepartment
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false, // 控制弹窗显示与隐藏
      curDate: null, // 点击哪一项就暂存进来
      codeList: [] // 存放所有code的数组
    }
  },
  mounted() {
    // 调用获取树形数据的方法
    this.loadDepartmentList()
  },
  methods: {
    // 获取树形数据
    async loadDepartmentList() {
      // 1.准备接口参数
      // 2.调用api接口函数
      const res = await getDepartments()
      // console.log(res)
      // 根据所有的源数据 把里面的code单独拎出来  组成一个新的数组 map()
      const sourceData = res.data.depts
      this.codeList = sourceData.map((item) => {
        return item.code
      })
      this.list = transTreeData(res.data.depts)
    },
    // 删除数据
    delDepartments(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 调用删除数据的接口
        await delDepartments(id)
        // 重新获取数据,刷新数组中的数据
        this.loadDepartmentList()
        // 提示用户
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
      })
    },
    // 点击弹窗x 关闭弹窗
    closeDialog() {
      this.showDialog = false
    },
    // 点击新增子部门 弹窗显示
    addDepartment(curDate) {
      this.curDate = curDate
      this.showDialog = true
    },
    // 点击编辑按钮
    editDepartment(curDate) {
      // 1. 弹窗打开
      this.showDialog = true
      // 2. 发送请求, 获取当前所在项的数据 回显
      // 通过ref获取到组件的实例, 调取里面的方法 获取当前所在项的数据
      this.$refs.editDep.loadEditDeparment(curDate.id)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
